package StudyCode.summary.zipfile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CreateZipFileFromMultipleFilesUsingZipOutputStream {
	public static void main(String args[]) {
		try {
			String zipFile = "zipdemo3.zip";
			String[] sourceFiles = {"E:/Copy/1.txt", "E:/Copy/2.txt"};
			byte[] buffer = new byte[1024];
			FileOutputStream fout = new FileOutputStream(zipFile);
			ZipOutputStream zout = new ZipOutputStream(fout);
			for (int i = 0; i < sourceFiles.length; i++) {
				System.out.println("添加 " + sourceFiles[i]);
				FileInputStream fin = new FileInputStream(sourceFiles[i]);
				zout.putNextEntry(new ZipEntry(sourceFiles[i]));
				int length;

				while ((length = fin.read(buffer)) > 0) {
					zout.write(buffer, 0, length);
				}
				zout.closeEntry();
				fin.close();
			}
			zout.close();
			System.out.println("zip 文件创建成功!");
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

	}
}
